Merged
Conversation
…aligned_layer into rework_batcher_concurrency
Co-authored-by: MauroFab <maurotoscano2@gmail.com>
JuArce
approved these changes
Aug 20, 2025
crates/batcher/Cargo.toml
Outdated
| ciborium = "=0.2.2" | ||
| priority-queue = "2.1.0" | ||
| reqwest = { version = "0.12", features = ["json"] } | ||
| dashmap = "6.0.1" |
MauroToscano
commented
Aug 20, 2025
MarcosNicolau
approved these changes
Aug 20, 2025
Member
MarcosNicolau
left a comment
There was a problem hiding this comment.
Looks good to me, the general code is much easier to follow through.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rework batcher concurrency
This PR overhauls the concurrency of the batcher, to optimize proofs processing and avoid race conditions
Description
Two locks are added, one for the batcher and one for the user states. The key things that are done are:
The biggest change is in the batch creation:
Notice we tolerate that the user state to be temporarily inconsistent with the queue until we confirm the block. This means that we are a bit stricter than we could with proofs the user send in parallel until we confirm the posting. (we assume the proofs is still in the queue and is not paid, so the user may need a bit more of spare balance and cannot use arbitrary fees, since we ask for the proofs in the batch to have the same or lower fee if have a bigger nonce)
For proof submission, the general idea is:
The added complexity is handling the case of a full queue. Since we cannot be sure that we can take a user lock after the batch lock, what we do is:
This mechanism avoids deadlocks, since it may happen that the "candidate" for eviction has it's state locked.
As a downside, it may be imprecise, since the user may need to bid more than N proofs, but it's not critical.
Another approach would be to briefly take the batch lock, peek to see if the queue is full, drop it and try to get the lock of the user with the least amount of fees. But this may lead to some edge cases that are harder to handle.
*The only exception to this rule is when we got a failure on sending a batch, in this scenario, to recover, we need to lock all the users states since the queue is finite and we may need to evict them and update their nonces. We don't actually take all the locks, and we may only need a couple. But to avoid a deadlock, we added a flag to avoid processing more users when a recovery is in progress which works in a similar manner. In the rare event that the lock we need is taken, the user task will timeout in 15s and release it for the restoration task
Type of change
Please delete options that are not relevant.